#include "CMyAnimInfo.h"

using namespace cocos2d;

CMyAnimInfo::CMyAnimInfo(void)
{
	m_pFrameList = NULL;
	m_nCurrFrame = 0;
	m_nFrames = 0;
	m_fFrameRate = 0.1f;
	m_fLastTime = 0.0f;
	m_bLoop = false;
	m_bPlay = false;
	m_pName = (char *)malloc(strlen(DEF_ANIM_NAME)+1);
	strcpy (m_pName, DEF_ANIM_NAME);
	m_pMoveStep = NULL;
	m_bCopyData = false;
}

CMyAnimInfo::CMyAnimInfo(const char *pName)
{
	m_pFrameList = NULL;
	m_nCurrFrame = 0;
	m_nFrames = 0;
	m_fFrameRate = 0.1f;
	m_fLastTime = 0.0f;
	m_bLoop = false;
	m_bPlay = false;
	m_pMoveStep = NULL;
	m_bCopyData = false;

	m_pName = (char *)malloc(strlen(pName)+1);
	strcpy (m_pName, pName);
	m_bCopyData = false;
}

CMyAnimInfo::~CMyAnimInfo(void)
{
	if(!m_bCopyData)
	{
		CC_SAFE_DELETE_ARRAY(m_pFrameList);
	}
	free (m_pName);
	if (m_pMoveStep)
	{
		// TO DO : release
	}
}

void CMyAnimInfo::init(const char *pFilename, int fWidth, int fHeight, int nframe, float rate, bool multi_file, bool loop, bool framecache)
{
	char szFullName[80];

	CC_SAFE_DELETE_ARRAY(m_pFrameList);
	m_pFrameList = new CCArray;
	m_nFrames = nframe;
	m_fFrameRate = rate;
	m_bLoop = loop;
	/* TO DO : setup
	m_pMoveStep = new CCPoint*[m_nFrames];
	for (int i = 0; i < m_nFrames; i++)
	{
		m_pMoveStep [i] = new CCPoint;
	}
	*/
	if (framecache)
	{
		CCSpriteFrameCache *pCache = CCSpriteFrameCache::sharedSpriteFrameCache();
		CCSpriteFrame *pAllFrame = NULL;

		if (!multi_file)
		{
			sprintf(szFullName, "%s.png", pFilename);
			pAllFrame = pCache->spriteFrameByName(szFullName);
		}
		for (int i = 0; i < nframe; i++)
		{
			CCSpriteFrame *pFrame = NULL;
			if (multi_file)
			{
				sprintf(szFullName, "%s%d.png", pFilename, i);
				pFrame = pCache->spriteFrameByName(szFullName);
			}
			else
			{
				pFrame = CCSpriteFrame::createWithTexture(pAllFrame->getTexture(),
						CCRectMake(fWidth*i, fHeight*0, fWidth, fHeight));
			}
			m_pFrameList->addObject(pFrame);
		}
	}
	else
	{
		CCTexture2D *texture = NULL;
		if (!multi_file)
		{
			sprintf(szFullName, "%s.png", pFilename);
			texture = CCTextureCache::sharedTextureCache()->addImage(szFullName);
		}
		for (int i = 0; i < nframe; i++)
		{
			CCSpriteFrame *pFrame = NULL;
			if (multi_file)
			{
				sprintf(szFullName, "%s%d.png", pFilename, i);
				texture = CCTextureCache::sharedTextureCache()->addImage(szFullName);
				CCRect rect = CCRectZero;
				rect.size = texture->getContentSize();
				pFrame = CCSpriteFrame::createWithTexture(texture, rect);
			}
			else
			{
				pFrame = CCSpriteFrame::createWithTexture(texture,
						CCRectMake(fWidth*i, fHeight*0, fWidth, fHeight));
			}
			m_pFrameList->addObject(pFrame);
		}
	}
	reset();
}

bool CMyAnimInfo::nextFrame(void)
{
	if (!m_bPlay) return false;

	float elapsed = getTimeTick() - m_fLastTime;
	if (elapsed >= m_fFrameRate)
	{
		m_fLastTime = getTimeTick();
		if (m_nCurrFrame == m_nFrames - 1)
		{
			if (m_bLoop)
			{
				m_nCurrFrame = 0;
				return true;
			}
		}
		else
		{
			m_nCurrFrame ++;
			return true;
		}
	}
	return false;
}

void CMyAnimInfo::reset(void)
{
	m_nCurrFrame = 0;
	m_fLastTime = getTimeTick();
}

bool CMyAnimInfo::isAniOver(void)
{
	if (m_bLoop) return false;
	if (m_nCurrFrame < m_nFrames - 1) return false;
	return true;

}

void CMyAnimInfo::setPlay(bool bPlay)
{
	m_bPlay = bPlay;
	m_fLastTime = getTimeTick();
}

CCSpriteFrame* CMyAnimInfo::getCurrentFrame(void)
{
	return (CCSpriteFrame *)m_pFrameList->objectAtIndex(m_nCurrFrame);
}

CCPoint* CMyAnimInfo::getNextMoveStep(void)
{
	if (m_nFrames < 2) return NULL;
	return m_pMoveStep[m_nCurrFrame];
}

// A portable way to get millisecs
float CMyAnimInfo::getTimeTick(void)
{
  timeval time;

  gettimeofday(&time, NULL);
  unsigned long millisecs = (time.tv_sec * 1000) + (time.tv_usec / 1000);

  //CCLog("getTimeTick %d", millisecs);
  return (float) millisecs;
}  

CCObject* CMyAnimInfo::copyWithZone(cocos2d::CCZone *pZone)
{
	CMyAnimInfo *pCopy = new CMyAnimInfo(m_pName);

	pCopy->initDataCopy(m_pFrameList, m_nFrames, m_fFrameRate, m_bLoop, m_pName);
	return pCopy;
}

void CMyAnimInfo::initDataCopy(CCArray *pFrameList, int nFrames, float rate, bool bLoop, char *pName)
{
	m_pFrameList = pFrameList;
	m_nFrames = nFrames;
	m_fFrameRate = rate;
	m_bLoop = bLoop;
	strcpy(m_pName, pName);
	m_bCopyData = true;
}
